package com.jbp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.IdentifyDao;
import com.jbp.exception.RRException;
import com.jbp.model.Identify;
import com.jbp.service.IdentifyService;
import com.jbp.service.TencentIdentifyService;
import com.jbp.utils.IdNumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * 实名认证(Identify)表服务实现类
 * @author cc
 * @since 2020-06-20 21:43:48
 */
@Service
public class IdentifyServiceImpl extends ServiceImpl<IdentifyDao, Identify> implements IdentifyService {
    @Autowired
    private TencentIdentifyService tencentIdentifyService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<Identify> page = this.page(
                new Query<Identify>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    /**保存实名信息*/
    @Override
    public void saveIdNumber(Integer userId, Identify req) {
        ///身份证是否合法
        if (!IdNumberUtil.isIDNumber(req.getIdNumber())) {
            throw new RRException("身份证格式错误");
        }
        ///是否认证过
        if (isIdentify(userId)) {
            throw new RRException("已经认证过了");
        }
        ///保存认证信息
        Identify identify = new Identify();
        identify.setUserId(userId);
        identify.setRealName(req.getRealName());
        identify.setIdNumber(req.getIdNumber());
        save(identify);
    }

    /**是否实名认证过*/
    @Override
    public boolean isIdentify(Integer userId){
        QueryWrapper<Identify> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("status", 2);

        return count(queryWrapper)>0;
    }

    @Override
    public Identify getByUserId(Integer userId) {
        QueryWrapper<Identify> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return getOne(queryWrapper);
    }

    @Override
    public String creatOrderNo(Identify identify) {
        return identify.getId()+"-"+System.currentTimeMillis()/1000;
    }

    @Override
    @Transactional
    public Map<String, Object> creatOrder(Identify identify, Integer userId) {
        ///身份证是否合法
        if (!IdNumberUtil.isIDNumber(identify.getIdNumber())) {
            throw new RRException("身份证格式错误");
        }
        ///是否认证过
        Identify result = getByUserId(userId);
        if (result!=null) {
            if (result.getStatus()==2) {
                throw new RRException("已经认证过了");
            } else if (result.getStatus()==1) {
                throw new RRException("认证中，请稍后");
            } else {
                result.setIdNumber(identify.getIdNumber());
                result.setRealName(identify.getRealName());
                updateById(result);
                identify = result;
            }
        } else {
            identify.setUserId(userId);
            save(identify);
        }

        identify.setStatus(0);
        identify.setOrderNo(creatOrderNo(identify));
        updateById(identify);

        Map<String, Object> map = tencentIdentifyService.createOrder(identify);
        return map;
    }

    @Override
    public void succes(Integer userId) {
        Identify identify = getByUserId(userId);
        if (identify==null) {
            throw new RRException("认证失败");
        }
        identify.setStatus(2);
        updateById(identify);
    }
}